package com.mzj.saas.mservice.smartlock.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.haixiaoke.saas.repository.pojo.SmartLock;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.commons.enums.LockOperatorTypeEnum;
import com.mzj.saas.mservice.lock.Repository.SmartLockBleRepository;
import com.mzj.saas.mservice.lock.Repository.SmartLockCardRepository;
import com.mzj.saas.mservice.lock.Repository.SmartLockPwdRepository;
import com.mzj.saas.mservice.lock.entity.SmartLockBle;
import com.mzj.saas.mservice.lock.entity.SmartLockCard;
import com.mzj.saas.mservice.lock.entity.SmartLockFace;
import com.mzj.saas.mservice.lock.entity.SmartLockPwd;
import com.mzj.saas.mservice.smartlock.service.haixiaoke.SmartLockHxkService;
import com.mzj.saas.mservice.smartlock.service.huixiangjia.SmartLockHxjService;
import com.mzj.saas.mservice.smartlock.service.luoke.SmartLockLkService;
import com.mzj.saas.mservice.smartlock.service.ruanku.SmartLockService;
import com.mzj.saas.mservice.smartlock.service.tianwanghulian.BluetoothService;
import com.mzj.saas.mservice.smartlock.service.tianwanghulian.FourEventHandleService;
import com.mzj.saas.mservice.smartlock.service.tianwanghulian.SmartLockTWHlService;
import com.mzj.saas.mservice.smartlock.service.xizhao.SmartLockXzService;
import com.mzj.saas.mservice.smartlock.service.yaluete.SmartLockYltService;
import com.mzj.saas.mservice.smartlock.service.youdian.SmartLockYdEventHandleService;
import com.mzj.saas.mservice.smartlock.service.youdian.SmartLockYdService;
import com.mzj.saas.mservice.smartlock.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 * 不同品牌智能锁接口
 *
 * @author shun
 * @createDate: 2021年6月25日 下午4:45:44
 * @version: 1.0
 */
@Service
public class SmartLockBrandService {

    private static final Logger log = LoggerFactory.getLogger(SmartLockBrandService.class);

    @Autowired
    private SmartLockService smartLockService;
    @Autowired
    private SmartLockXzService smartLockXzService;
    @Autowired
    private SmartLockXzService smartLockApiService;
    @Autowired
    private SmartLockYltService smartLockYltService;
    @Autowired
    private SmartLockPwdRepository smartLockPwdRepository;

    @Autowired
    private BluetoothService bluetoothService;

    @Autowired
    private FourEventHandleService fourEventHandleService;
    @Autowired
    private SmartLockTWHlService smartLockTWHlService;
    @Autowired
    private SmartLockCardRepository smartLockCardRepository;
    @Autowired
    private SmartLockBleRepository smartLockBleRepository;
    @Autowired
    private SmartLockYdEventHandleService smartLockYdEventHandleService;
    @Autowired
    private SmartLockYdService smartLockYdService;
    @Autowired
    private SmartLockHxkService smartLockHxkService;
    @Autowired
    private SmartLockLkService smartLockLkService;
    @Autowired
    private SmartLockHxjService smartLockHxjService;


    /**
     * 根据mac获取设备sn号/熙照设备注册
     *
     * @param devMac  设备MAC
     * @param brandId 设备品牌id
     * @return
     * @author shun
     * @date 2021年6月25日
     */
    public ResultBean<String> getSnByMac(String devMac, String deviceName, Long brandId) {
        try {
            if (StringUtils.isBlank(devMac) || brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库智能锁
                    return smartLockService.getSnByMac(devMac);
                case "2":
                    // 熙照智能锁(无需获取SN), 此处调用注册接口, 成功后返回的sn = imei
                    // 熙照智能锁注册(没有查询sn接口，注册后事件返回sn，事件中会更新表sn字段)
                    // 注册后返回sn为空
                    return smartLockApiService.bindimei(devMac, deviceName, brandId);
                case "3":
                    // 亚略特无需SN，所有接口操作使用MAC = imei号
                    return smartLockYltService.merchantinfoBindLock(devMac, brandId);
                case "4":
                    // 天网互联
                    return fourEventHandleService.event(devMac, brandId);
                case "5":
                    SmartLock smartLock = new SmartLock();
                    smartLock.setSn(devMac);
                    smartLock.setPageNum(1);
                    smartLock.setPageSize(1);
                    ResultBean<String> resultBean = smartLockHxkService.selectLockList(smartLock);
                    JSONObject jsonObject = JSONObject.parseObject(resultBean.getResultData());
                    if (ObjectUtil.isEmpty(jsonObject)) {
                        return ResultBean.failedResultOfParamWithMsg("设备查询失败");
                    }
                    String result = jsonObject.getString("result");
                    if ("[]".equals(result)) {
                        return ResultBean.failedResultOfParamWithMsg("绑定的设备不存在");
                    }
                    return ResultBean.successfulResult(devMac);
                case "6":
                    // 优电
                    return smartLockYdEventHandleService.insertSmartLock(devMac);
                case "7":
                    // 络克
                    ResultBean<String> lockDetail = smartLockLkService.getLockDetail(devMac);
                    if (StatusCode.SUCCESS_CODE_10000.getErrorCode().equals(lockDetail.getCode())) {
                        JSONObject lockDetailJson = JSONObject.parseObject(lockDetail.getResultData());
                        return ResultBean.successfulResult(lockDetailJson.getString("deviceId"));
                    }
                case "8":
                    // 慧享佳
                    return smartLockHxjService.getLockDetail(devMac);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("根据mac获取设备sn号异常" + e.getMessage(), e);
        }

        return ResultBean.failedResultOfException();
    }

    /**
     * 批量mac获取设备sn号
     *
     * @param devMacs 批量设备sn号
     * @param brandId 设备品牌id
     * @return
     * @author shun
     * @date 2021年6月25日
     */
    public ResultBean<List<DevMacVO>> batchGetSnByMac(List<String> devMacs, Long brandId) {
        try {
            if (CollectionUtils.isEmpty(devMacs) || brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库智能锁
                    return smartLockService.batchGetSnByMac(devMacs);
                case "2":
                    // 熙照智能锁(无需获取SN)
                    break;
                case "3":
                    // 亚略特智能锁(无需获取SN)
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("批量mac获取设备sn号异常" + e.getMessage(), e);
        }

        return ResultBean.failedResultOfException();
    }

    /**
     * 获取锁状态
     *
     * @param id      锁SN或锁ID
     * @param brandId 设备品牌id
     * @return
     * @author shun
     * @date 2021年6月25日
     */
    public ResultBean<DevMacStateVO> getState(String id, Long brandId) {
        try {
            if (StringUtils.isBlank(id) || brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库智能锁
                    return smartLockService.getState(id);
                case "2":
                    // 熙照
                    return smartLockXzService.getState(id, brandId);
                case "4":
                    // 天网互联
                    return fourEventHandleService.deviceList(id, brandId);
                case "6":
                    // 优电
                    return smartLockYdService.deviceList(id);
                case "8":
                    // 慧享佳
                    return smartLockHxjService.getLockDetailInfo(id);
                case "3":
                    // 亚略特无此接口
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("获取锁状态异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }


    /**
     * 获取蓝牙密钥
     *
     * @param brandId 设备品牌id
     * @return
     * @author shun
     * @date 2021年6月25日
     */
    public ResultBean<String> BluetoothKey(Long lockAuthId, Long brandId, String sn) {
        try {
            if (lockAuthId == null || brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "4":
                    // 天网互联
                    return smartLockTWHlService.getBluetoothUnlocking(lockAuthId);
                case "7":
                    // 络克
                    return smartLockLkService.getBluetoothUnlocking(sn);
                case "8":
                    // 慧享佳
                    return smartLockHxjService.getBluetoothUnlocking(sn);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("获取蓝牙信息异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 批量获取锁状态
     *
     * @param
     * @return
     */
    public ResultBean<List<DevMacStateVO>> batchGetState(List<String> sns, Long brandId) {
        try {
            if (CollectionUtils.isEmpty(sns) || brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库智能锁
                    return smartLockService.batchGetState(sns);
                case "2":
                    //
                    // TODO

                    break;
                case "3":
                    // 亚略特无此接口
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("批量获取锁状态异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 查询动态密码
     *
     * @param vo
     * @return
     */
    public ResultBean<List<DynamicPasswordVO>> getDynamicPassword(SmartLockVO vo, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.getDynamicPassword(vo);
                case "2":
                    return smartLockXzService.getDynamicPassword(vo.getSn(), brandId);
                case "3":
                    // 暂时厂家未开通
                    return smartLockYltService.getUrgentPassword(vo.getSn(), brandId);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("查询动态密码异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 新增/修改密码
     *
     * @param vo
     * @param smartLockPwd
     * @param brandId
     * @return
     * @author shun
     * @date 2021年7月6日
     */
    public ResultBean<String> operationPassword(SmartLockVO vo, SmartLockPwd smartLockPwd, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.operationPassword(vo);
                case "2":
                    return smartLockXzService.validateOperationTypeAndPassword(smartLockPwd, vo, brandId);
                case "4":
                    // return fourEventHandleService.TimedPassword(vo, smartLockPwd, brandId);
                    return smartLockTWHlService.authorizationPwd(vo, smartLockPwd);
                case "5":
                    return smartLockHxkService.authorizationPwd(vo, smartLockPwd);
                case "6":
                    return smartLockYdService.authorizationPwd(vo, smartLockPwd);
                case "7":
                    return smartLockLkService.authorizationPwd(vo, smartLockPwd);
                case "8":
                    return smartLockHxjService.authorizationPwd(vo, smartLockPwd);
                case "3":

                    SmartLockPwd smartLockPwds = null;
                    if (smartLockPwd != null || smartLockPwd.getId() != null) {
                        smartLockPwds = smartLockPwdRepository.findByLockAuthId(smartLockPwd.getLockAuthId());

                    }

                    Date date = new Date();
                    long time = date.getTime();

                    // 新增
                    if (smartLockPwd == null || smartLockPwd.getId() == null) {
                        // 必须设置YltAuthorizationVO参数值
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    } else if (smartLockPwds.getEndTime() == null || time > smartLockPwds.getEndTime().getTime()) {
                        ResultBean<String> stringResultBean = smartLockYltService.lockAuthorization(brandId, vo);
                        String resultData = stringResultBean.getResultData();
                        String msg = stringResultBean.getMsg();
                        String code = stringResultBean.getCode();

                        if (!code.equals(StatusCode.SUCCESS_CODE_10000.getErrorCode())) {
                            log.error("operationPassword authPwd 更新亚略特授权ID失败---> " + msg);
                            return ResultBean.failedResultWithCodeAndMsg(code, msg);
                        } else {
                            log.info("operationPassword authPwd 更新亚略特授权ID成功---> callBack={}", resultData);
                            // 更新密码授权ID
                            smartLockPwd.setfAuthorizationId(resultData);

                        }

                    } else if (smartLockPwd.getId() != null) {
                        // 修改
                        if ("5".equals(vo.getAuthType())) {
                            return ResultBean.failedResultOfParamWithMsg("设备不支持修改次数");
                        }
                        // 只能更新密码
                        return smartLockYltService.updateAuthorize(vo, brandId);
                    }

                default:
                    break;
            }
        } catch (Exception e) {
            log.error("新增/修改密码异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 冻结/解冻密码
     *
     * @param vo
     * @param brandId
     * @param operatorType 1密码 2卡 3指纹
     * @return
     * @author shun
     * @date 2021年9月15日
     */
    public ResultBean<String> freezeOrUnfreeze(SmartLockVO vo, Long brandId, Integer operatorType) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.freezeOrUnfreeze(vo);
                case "2":
                    // 熙照智能锁, 1密码 2卡 3指纹
                    return smartLockXzService.freezeOrUnfreeze(vo, brandId, operatorType);
                case "3":
                    // 亚略特智能锁，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 取消授权
                        return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                    } else {
                        // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }
                case "4":
                    // 天网互联，删除授权密码
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        return smartLockTWHlService.deletePwd(vo, vo.getAuthInfoId());
                    } else {
                        // 天网互联，授权密码
                        Long authInfoId = vo.getAuthInfoId();
                        Integer pwdType = vo.getPwdType();
                        SmartLockPwd smartLockPwd = smartLockPwdRepository.findByLockAuthIdAndType(authInfoId, pwdType);

                        return smartLockTWHlService.authorizationPwd(vo, smartLockPwd);
                    }
                case "5":
                    // 嗨小克，删除授权密码
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        return smartLockHxkService.deletePwd(vo, vo.getAuthInfoId());
                    } else {
                        // 嗨小克，授权密码
                        Long authInfoId = vo.getAuthInfoId();
                        Integer pwdType = vo.getPwdType();
                        SmartLockPwd smartLockPwd = smartLockPwdRepository.findByLockAuthIdAndType(authInfoId, pwdType);

                        return smartLockHxkService.authorizationPwd(vo, smartLockPwd);
                    }
                case "6":
                    // 优电，删除授权密码
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        return smartLockYdService.deletePwd(vo, vo.getAuthInfoId());
                    } else {
                        // 优电，授权密码
                        Long authInfoId = vo.getAuthInfoId();
                        Integer pwdType = vo.getPwdType();
                        SmartLockPwd smartLockPwd = smartLockPwdRepository.findByLockAuthIdAndType(authInfoId, pwdType);

                        return smartLockYdService.authorizationPwd(vo, smartLockPwd);
                    }
                case "7":
                    // 络克，冻结授权密码
                    // 状态true：启用false：禁用
                    boolean stateFlag = vo.getSuspend() != 1;
                    return smartLockLkService.updateKeyState(vo.getIdentification(), stateFlag);
                case "8":
                    // 慧享佳，删除授权密码
                    if (vo.getSuspend() == 1) {
                        return smartLockHxjService.deletePwd(vo, vo.getAuthInfoId());
                    } else {
                        // 慧享佳，授权密码
                        Long authInfoId = vo.getAuthInfoId();
                        Integer pwdType = vo.getPwdType();
                        SmartLockPwd smartLockPwd = smartLockPwdRepository.findByLockAuthIdAndType(authInfoId, pwdType);

                        return smartLockHxjService.authorizationPwd(vo, smartLockPwd);
                    }
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("冻结/解冻密码异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 删除密码
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> clearPassword(SmartLockVO vo, Long brandId, String passwordType, Long lockAuthId) {
        try {
            // 校验品牌参数
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.clearPassword(vo);
                case "2":
                    // code 密码 adminpwd 管理员密码(删除不支持)
                    // dynamicpwd 动态密码(冻结、解冻、删除不支持)
                    // fingerprint 指纹
                    // card 卡片
                    return smartLockXzService.clearPassword(vo, brandId, passwordType);
                case "3":
                    // 亚略特只有取消授权
                    return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                case "4":
                    // 天网互联删除密码授权
                    return smartLockTWHlService.deletePwd(vo, lockAuthId);
                case "5":
                    return smartLockHxkService.deletePwd(vo, lockAuthId);
                case "6":
                    // 优电删除密码授权
                    return smartLockYdService.deletePwd(vo, lockAuthId);
                case "7":
                    // 络克删除密码授权
                    return smartLockLkService.deletePwd(vo, lockAuthId);
                case "8":
                    // 慧享佳删除密码授权
                    return smartLockHxjService.deletePwd(vo, lockAuthId);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除密码异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 清除锁所有密码(不包含动态密码（动态密码可以继续使用）。)
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> clearAllPassword(@RequestBody SmartLockVO vo, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.clearAllPassword(vo);
                case "2":
                    //
                    // TODO

                    break;
                case "3":
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("清除锁所有密码异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 新增/修改卡
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> operationCardPassword(SmartLockVO vo, SmartLockCard smartLockCard, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库
                    return smartLockService.operationCardPassword(vo);
                case "2":
                    // 熙照
                    return smartLockXzService.validateOperationTypeAndCardPwd(smartLockCard, vo, brandId);
                case "4":
                    // 天网互联
                    // return fourEventHandleService.doorCard(vo, smartLockCard, brandId);
                    return smartLockTWHlService.identityCardUnlocking(vo, smartLockCard);
                case "5":
                    return smartLockHxkService.identityCardUnlocking(vo, smartLockCard);
                case "6":
                    // 优电
                    return smartLockYdService.identityCardUnlocking(vo, smartLockCard);
                case "7":
                    // 络克
                    return smartLockLkService.identityCardUnlocking(vo, smartLockCard);
                case "8":
                    // 慧享佳
                    return smartLockHxjService.identityCardUnlocking(vo, smartLockCard);
                case "3":
                    // 亚略特
                    // 新增
                    if (smartLockCard.getId() == null) {
                        // 必须设置YltAuthorizationVO参数值
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }

                    // 次数不能修改
                    if ("5".equals(vo.getAuthType())) {
                        return ResultBean.failedResultOfParamWithMsg("设备不支持修改次数");
                    }

                    /**
                     * 不支持卡更新，只能删除卡重新授权
                     */

                    // 取消授权
                    smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());

                    // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                    return smartLockYltService.lockAuthorization(brandId, vo);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("新增/修改卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 冻结/解冻卡
     *
     * @param vo
     * @param brandId
     * @param operatorType 熙照智能锁, 1密码 2卡 3指纹
     * @return
     * @author shun
     * @date 2021年7月2日
     */
    public ResultBean<String> freezeOrUnfreezeCard(SmartLockVO vo, Long brandId, Integer operatorType) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.freezeOrUnfreezeCard(vo);
                case "2":
                    return smartLockXzService.freezeOrUnfreeze(vo, brandId, operatorType);
                case "3":
                    // 亚略特智能锁，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 取消授权
                        return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                    } else {
                        // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }
                case "4":
                    // 天网互联，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 天网互联删除身份证/CPU卡开锁
                        return smartLockTWHlService.deleteIdentityCardUnlocking(vo);
                    } else {
                        // 天网互联授权身份证/CPU卡开锁
                        Long authInfoId = vo.getAuthInfoId();
                        Integer cardType = vo.getCardType();
                        SmartLockCard smartLockCard = smartLockCardRepository.findByLockAuthIdAndType(authInfoId, cardType);
                        return smartLockTWHlService.identityCardUnlocking(vo, smartLockCard);
                    }
                case "5":
                    // 嗨小克，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 天网互联删除身份证/CPU卡开锁
                        return smartLockHxkService.deleteIdentityCardUnlocking(vo);
                    } else {
                        // 天网互联授权身份证/CPU卡开锁
                        Long authInfoId = vo.getAuthInfoId();
                        Integer cardType = vo.getCardType();
                        SmartLockCard smartLockCard = smartLockCardRepository.findByLockAuthIdAndType(authInfoId, cardType);
                        return smartLockHxkService.identityCardUnlocking(vo, smartLockCard);
                    }
                case "6":
                    // 优电，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 优电删除身份证/CPU卡开锁
                        return smartLockYdService.deleteIdentityCardUnlocking(vo);
                    } else {
                        // 优电授权身份证/CPU卡开锁
                        Long authInfoId = vo.getAuthInfoId();
                        Integer cardType = vo.getCardType();
                        SmartLockCard smartLockCard = smartLockCardRepository.findByLockAuthIdAndType(authInfoId, cardType);
                        return smartLockYdService.identityCardUnlocking(vo, smartLockCard);
                    }
                case "7":
                    // 络克,冻结解冻门卡
                    // 状态true：启用false：禁用
                    boolean stateFlag = vo.getSuspend() != 1;
                    return smartLockLkService.updateKeyState(vo.getIdentification(), stateFlag);
                case "8":
                    // 慧享佳，没有冻结操作，只能做取消授权
                    if (vo.getSuspend() == 1) {
                        // 慧享佳删除身份证/CPU卡开锁
                        return smartLockHxjService.deleteIdentityCardUnlocking(vo);
                    } else {
                        // 慧享佳授权身份证/CPU卡开锁
                        Long authInfoId = vo.getAuthInfoId();
                        Integer cardType = vo.getCardType();
                        SmartLockCard smartLockCard = smartLockCardRepository.findByLockAuthIdAndType(authInfoId, cardType);
                        return smartLockHxjService.identityCardUnlocking(vo, smartLockCard);
                    }
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("冻结/解冻卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 删除卡
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> clearCard(SmartLockVO vo, Long brandId, String passwordType) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.clearCard(vo);
                case "2":
                    // code 密码 adminpwd 管理员密码(删除不支持)
                    // dynamicpwd 动态密码(冻结、解冻、删除不支持)
                    // fingerprint 指纹
                    // card 卡片
                    return smartLockXzService.clearPassword(vo, brandId, passwordType);
                case "3":
                    // 亚略特只有取消授权
                    return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                case "4":
                    // 天网互联删除身份证授权
                    return smartLockTWHlService.deleteIdentityCardUnlocking(vo);
                case "5":
                    return smartLockHxkService.deleteIdentityCardUnlocking(vo);
                case "6":
                    // 优电删除门卡授权
                    return smartLockYdService.deleteIdentityCardUnlocking(vo);
                case "7":
                    // 络克删除门卡授权
                    return smartLockLkService.deleteIdentityCardUnlocking(vo);
                case "8":
                    // 慧享佳删除门卡授权
                    return smartLockHxjService.deleteIdentityCardUnlocking(vo);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 删除所有卡
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> clearAllCard(@RequestBody SmartLockVO vo, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return smartLockService.clearAllCard(vo);
                case "2":
                    //
                    // TODO

                    break;
                case "3":
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除所有卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 指纹新增/修改
     *
     * @param vo
     * @return 回调id
     */
    public ResultBean<String> operationFingerprint(SmartLockFingerprintVO vo, Long fingerprintId, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "2":
                    // 熙照
                    return smartLockXzService.validateOperationFingerprint(vo, fingerprintId, brandId);
                case "3":
                    // 亚略特
                    // 新增
                    SmartLockVO cvLockVO = new SmartLockVO();
                    cvLockVO.setYltAuth(vo.getYltAuth());
                    if (fingerprintId == null) {
                        // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                        return smartLockYltService.lockAuthorization(brandId, cvLockVO);
                    }

                    // 次数不能修改
                    if ("5".equals(vo.getAuthType())) {
                        return ResultBean.failedResultOfParamWithMsg("设备不支持修改次数");
                    }

                    /**
                     * 不支持卡更新，只能删除卡重新授权
                     */
                    // 取消授权
                    cvLockVO.setSn(vo.getSn());
                    cvLockVO.setfAuthorizationID(vo.getfAuthorizationID());
                    smartLockYltService.deauthorize(cvLockVO, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());

                    // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                    return smartLockYltService.lockAuthorization(brandId, cvLockVO);
                case "4":
                    // 天网互联授权指纹信息
                    return smartLockTWHlService.fingerprintUnlocking(vo);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("新增指纹异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 删除指纹
     *
     * @param vo
     * @param brandId
     * @param passwordType
     * @return
     * @author shun
     * @date 2021年7月7日
     */
    public ResultBean<String> clearFingerprint(SmartLockVO vo, Long brandId, String passwordType) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "2":
                    // code 密码 adminpwd 管理员密码(删除不支持)
                    // dynamicpwd 动态密码(冻结、解冻、删除不支持)
                    // fingerprint 指纹
                    // card 卡片
                    return smartLockXzService.clearPassword(vo, brandId, passwordType);
                case "3":
                    // 取消授权
                    return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                case "4":
                    // 天网互联删除指纹数据
                    return smartLockTWHlService.deleteFingerprintUnlocking(vo);
                case "6":
                    return ResultBean.successfulResult("成功");
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 冻结/解冻指纹
     *
     * @param vo
     * @param brandId
     * @param operatorType 熙照智能锁, 1密码 2卡 3指纹
     * @return
     * @author shun
     * @date 2021年7月2日
     */
    public ResultBean<String> freezeOrUnfreezeFingerprint(SmartLockVO vo, Long brandId, Integer operatorType) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    break;
                case "2":
                    return smartLockXzService.freezeOrUnfreeze(vo, brandId, operatorType);
                case "3":
                    // 亚略特智能锁，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 取消授权
                        return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                    } else {
                        // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }
                case "4":
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 天网互联删除指纹开锁
                        return smartLockTWHlService.deleteFingerprintUnlocking(vo);
                    } else {
                        // 天网互联授权指纹开锁
                        SmartLockFingerprintVO smartLockFingerprintVO = new SmartLockFingerprintVO();
                        smartLockFingerprintVO.setFingerprint(vo.getFingerprint());
                        smartLockFingerprintVO.setIdentification(vo.getIdentification());
                        smartLockFingerprintVO.setSn(vo.getSn());
                        smartLockFingerprintVO.setAuthType(vo.getAuthType());
                        return smartLockTWHlService.fingerprintUnlocking(smartLockFingerprintVO);
                    }
                case "5":
                    return ResultBean.successfulResult("成功");
                case "6":
                    // 优电没有指纹授权
                    return ResultBean.successfulResult("成功");
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("冻结/解冻卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 授权蓝牙
     *
     * @param vo
     * @param smartLockBle
     * @param brandId
     * @return
     */
    public ResultBean<String> operationBle(SmartLockVO vo, SmartLockBle smartLockBle, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    // 软库
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "2":
                    // 熙照
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "4":
                    // 天网互联
                    // return bluetoothService.empower(vo.getSn(),smartLockBle.getAuthType(),smartLockBle);
                    return smartLockTWHlService.bluetoothUnlocking(vo, smartLockBle);
                case "7":
                    // 络克，无需授权
                    return ResultBean.successfulResult("成功");
                case "8":
                    // 慧享佳，无需授权
                    return ResultBean.successfulResult("成功");
                case "3":
                    // 亚略特
                    // 新增
                    if (smartLockBle.getId() == null) {
                        // 必须设置YltAuthorizationVO参数值
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }

                    // 次数不能修改
                    if ("5".equals(vo.getAuthType())) {
                        return ResultBean.failedResultOfParamWithMsg("设备不支持修改次数");
                    }

                    // 不支持蓝牙更新，只能删除重新授权
                    // 取消授权
                    smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());

                    // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                    return smartLockYltService.lockAuthorization(brandId, vo);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("新增蓝牙异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 删除蓝牙授权
     *
     * @param vo
     * @param brandId
     * @return
     */
    public ResultBean<String> clearBle(SmartLockVO vo, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "2":
                    return ResultBean.failedResultOfParamWithMsg("设备不支持");
                case "3":
                    // 取消授权
                    return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                case "4":
                    // 天网互联删除蓝牙开锁权限
                    return smartLockTWHlService.deleteBluetoothUnlocking(vo);
                case "6":
                    return ResultBean.successfulResult("成功");
                case "8":
                    // 慧享佳无需授权
                    return ResultBean.successfulResult("成功");
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除蓝牙异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 冻结/解冻蓝牙
     *
     * @param vo
     * @param brandId
     * @return
     */
    public ResultBean<String> freezeOrUnfreezeBle(SmartLockVO vo, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    break;
                case "2":
                    break;
                case "3":
                    // 亚略特智能锁，没有冻结操作，只能做取消授权
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 取消授权
                        return smartLockYltService.deauthorize(vo, brandId, LockOperatorTypeEnum.GENERAL_UN_AUTH.getValue());
                    } else {
                        // 授权，返回授权后新的AuthorizationID，需要同步更新到授权表中
                        return smartLockYltService.lockAuthorization(brandId, vo);
                    }
                case "4":
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 天网互联删除蓝牙开锁
                        return smartLockTWHlService.deleteBluetoothUnlocking(vo);
                    } else {
                        // 天网互联授权蓝牙开锁
                        Long authInfoId = vo.getAuthInfoId();
                        SmartLockBle smartLockBle = smartLockBleRepository.findByLockAuthId(authInfoId);
                        return smartLockTWHlService.bluetoothUnlocking(vo, smartLockBle);
                    }
                case "5":
                    return ResultBean.successfulResult("成功");
                case "6":
                    //    优电没有蓝牙授权
                    return ResultBean.successfulResult("成功");
                case "8":
                    //    慧享佳无需授权
                    return ResultBean.successfulResult("成功");
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("冻结/解冻卡异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 授权人脸信息
     *
     * @param smartLockFace 人脸信息
     * @param brandId
     * @return
     */
    public ResultBean<String> operationFace(SmartLockFace smartLockFace, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                case "2":
                case "3":
                case "4":
                    return ResultBean.failedResultWithMsg("该锁不支持人脸授权");
                case "5":
                    return smartLockHxkService.addFaceImage(smartLockFace);
                case "6":
                    return smartLockYdService.addFaceImage(smartLockFace);
                case "7":
                    return smartLockLkService.addFaceImage(smartLockFace);
                case "8":
                    return smartLockHxjService.addFaceImage(smartLockFace);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("授权人脸异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }


    /**
     * 修改授权人脸信息
     *
     * @param newSmartlockface 人脸信息
     * @param brandId
     * @return
     */
    public ResultBean<String> updateFace(SmartLockFace newSmartlockface, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "5":
                    return smartLockHxkService.updateFaceImage(newSmartlockface);
                case "6":
                    return smartLockYdService.updateFaceImage(newSmartlockface);
                case "7":
                    return smartLockLkService.updateFaceImage(newSmartlockface);
                case "8":
                    return smartLockHxjService.updateFaceImage(newSmartlockface);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("授权人脸异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }


    /**
     * 删除授权人脸信息
     *
     * @param smartLockFace 授权人脸信息
     * @param brandId       品牌id
     * @return
     */
    public ResultBean<String> clearFace(SmartLockFace smartLockFace, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "5":
                    return smartLockHxkService.deleteFaceImage(smartLockFace);
                case "6":
                    return smartLockYdService.deleteFaceImage(smartLockFace);
                case "7":
                    return smartLockLkService.deleteFaceImage(smartLockFace);
                case "8":
                    return smartLockHxjService.deleteFaceImage(smartLockFace);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("删除人脸异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();
    }

    /**
     * 冻结/解冻人脸授权信息
     *
     * @param vo
     * @param face    人脸授权信息
     * @param brandId 品牌id
     * @return
     */
    public ResultBean<String> freezeOrUnfreezeFace(SmartLockVO vo, SmartLockFace face, Long brandId) {
        try {
            if (brandId == null) {
                return ResultBean.failedResultOfParamWithMsg("参数错误");
            }

            switch (brandId.toString()) {
                case "1":
                    break;
                case "2":
                    break;
                case "3":
                    break;
                case "4":
                    break;
                case "5":
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 嗨小克删除人脸开锁
                        return smartLockHxkService.deleteFaceImage(face);
                    } else {
                        // 嗨小克授权人脸开锁
                        return smartLockHxkService.addFaceImage(face);
                    }
                case "6":
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 优电删除人脸开锁
                        return smartLockYdService.deleteFaceImage(face);
                    } else {
                        // 优电授权人脸开锁
                        return smartLockYdService.addFaceImage(face);
                    }
                case "7":
                    // 冻结（1）或者解冻（0)
                    // 状态true：启用false：禁用
                    boolean stateFlag = vo.getSuspend() != 1;
                    return smartLockLkService.updateKeyState(face.getIdentification(), stateFlag);
                case "8":
                    // 冻结（1）或者解冻（0)
                    if (vo.getSuspend() == 1) {
                        // 慧享佳删除人脸开锁
                        return smartLockHxjService.deleteFaceImage(face);
                    } else {
                        // 慧享佳授权人脸开锁
                        return smartLockHxjService.addFaceImage(face);
                    }
                default:
                    break;
            }

        } catch (Exception e) {
            log.error("冻结/解冻人脸异常" + e.getMessage(), e);
        }
        return ResultBean.failedResultOfException();


    }

    public String selectLockList(SmartLock smartLock) {
        ResultBean<String> resultBean = smartLockHxkService.selectLockList(smartLock);
        String result = "";
        if (StatusCode.SUCCESS_CODE_10000.getErrorCode().equals(resultBean.getCode())) {
            result = resultBean.getResultData();
        }
        return result;
    }

    /**
     * 离线密码
     *
     * @param vo
     * @param brandId
     * @return
     */
    public ResultBean<Object> offlinePwd(SmartLockVO vo, Long brandId) {
        if (brandId == null) {

            return ResultBean.failedResultOfParamWithMsg("参数异常");
        }
        switch (brandId.toString()) {
            case "1":
                break;
            case "2":
                break;
            case "3":
                break;
            case "4":
                break;
            case "5":
                return smartLockHxkService.offlinePwd(vo);
            case "6":
            default:
                break;
        }
        return ResultBean.failedResult("智能锁型号异常");

    }
}
